package usw.webserver;

import java.net.Socket;

import org.apache.log4j.Logger;

import usw.webserver.entity.HttpRequest;
import usw.webserver.entity.HttpResponse;


/**
 *
 */
public class RequestHandler implements Runnable {

	private static Logger log = Logger.getLogger(RequestHandler.class);

	private Socket socket;
	private String webRootPath;
	
	/**
	 * 
	 * @param socket
	 * @param webRootPath
	 */
	public RequestHandler(Socket socket, String webRootPath) {
	
		this.socket = socket;
		this.webRootPath = webRootPath;
	}

	/**
	 * 
	 */
	public void run() 
	{
		try 
		{
			HttpRequest req = new HttpRequest(socket.getInputStream());
			
			log.info("Request Method:" + req.method.toString());
			
			HttpResponse res = new HttpResponse(req, this.webRootPath);
			res.write(socket.getOutputStream());
			socket.close();
			
		} 
		catch (Exception e) 
		{
			log.error("Runtime Error", e);
		}
	}
}
